package com.erpoa.controller;

import com.alibaba.fastjson.JSON;
import com.erpoa.dto.DealMessage;
import com.erpoa.entity.PackMessageByWuLiu;
import com.erpoa.service.CarProgressService;
import com.erpoa.service.SessionManager;
import com.erpoa.utils.PDAException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;

/**
 * @author chenjimin
 * @Title: CarProgressController
 * @ProjectName oaerp
 * @Description: TODO
 * @date 2020/11/215:00
 */
@CrossOrigin(maxAge = 3600)
@Api(value = "CarProgressController", tags = {"物流中心装车进度接口"})
@RestController
@RequestMapping("/PDAService")
public class CarProgressController {
    private static final Logger logger = LoggerFactory.getLogger(CarProgressController.class);

    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private CarProgressService CarProgressService;

    /**
     * wuLiuFahuoServlet
     *
     * @return
     * @throws Exception
     */
    @ApiOperation("物流中心装车进度接口")
    @RequestMapping(value = {"/carProgressServlet"}, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public DealMessage carProgressServlet(@RequestBody PackMessageByWuLiu packMessage) {
        DealMessage dealMessage = new DealMessage(true);
        int optType = packMessage.getOptType();

        try {
            logger.debug("线程:" + Thread.currentThread().getId() + "请求开始：" + packMessage.toString());
            switch (optType) {
                case 0:
                    dealMessage.setMessage(JSON.toJSONString(CarProgressService.selectByTrayNo(packMessage)));
                    break;
                case 1:
                    dealMessage.setMessage(JSON.toJSONString(CarProgressService.addtraynumber(packMessage)));
                    break;
                case 2:
                    dealMessage.setMessage(JSON.toJSONString(CarProgressService.getCarNo(packMessage)));
                    break;
                case 3:
                    dealMessage.setMessage(JSON.toJSONString(CarProgressService.selectuntrayNum(packMessage)));
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof PDAException) {
                dealMessage = new DealMessage(false, e.getMessage());
            } else if (e instanceof DuplicateKeyException) {
                throw new PDAException("数据重复，请重新扫描！");

            } else if (e instanceof SQLException) {
                throw new PDAException("数据重复，请重新扫描！");
            } else {
                dealMessage = new DealMessage(false, "数据处理出现未知错误，请再扫一次确认结果，如多次出现请及时反馈IT，分机号：6257！时间点：" + System.currentTimeMillis() + "错误信息：" + e.getMessage());
            }
            logger.debug("线程:" + Thread.currentThread().getId() + "请求结束：" + dealMessage.toString());
            return dealMessage;
        }
        logger.debug("线程:" + Thread.currentThread().getId() + "请求结束：" + dealMessage.toString());
        return dealMessage;
    }
}
